package com.spider.service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

import com.spider.dao.HBaseDAOImp;
import com.spider.dao.HBaseDAl;
import com.spider.dao.HBaseDao;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.FileCacheQueueScheduler;

public class GetAnswer implements PageProcessor{
	
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);
	private HBaseDao dao = new HBaseDAOImp();
	
	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}
	
	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
        page.putField("answer",page.getHtml().xpath("//div[@class='show-box answer']"));
        page.putField("url", page.getUrl());
        String content = page.getResultItems().get("answer").toString();
        String url = page.getResultItems().get("url").toString();
        String rowKey = url.substring("http://k12.tiku.com/question.html?sct=0&cn=����&st=2&id=".length(), "http://k12.tiku.com/question.html?sct=0&cn=����&st=2&id=62431310".length());
        dao.insert("answer", rowKey, "base", "answer", content);
	}
	
	 public Map<String,String> getUrl() {
        List<Result> list = new HBaseDAl().readTestsAnswerUrl();
        Map<String,String> urlMap = new LinkedHashMap<String,String>();
        for (Result result : list) {
            for (KeyValue key : result.raw()) {            
                urlMap.put(Bytes.toString(key.getRow()),Bytes.toString(key.getValue()).replaceAll("www","k12"));
            }
        }
        return urlMap;
    }
	
	public static void main(String[] args) {
//		GetAnswer anser = new GetAnswer();
//		List<String> list = new ArrayList();
//		Map urlMap = (LinkedHashMap<String, String>)anser.getUrl();
//		Iterator iter = urlMap.entrySet().iterator();
//		int index = 0;
//        while(iter.hasNext()) {
//        	Map.Entry entry = (Map.Entry) iter.next();
//        	list.add(entry.getValue().toString());
//        	System.out.println(entry.getValue().toString());
//        }
		
		Spider spider = Spider.create(new GetAnswer())
				.addUrl("http://k12.tiku.com/question.html?sct=0&cn=����&st=2&id=62243814&courseId=500011")
				.setScheduler(new FileCacheQueueScheduler("./answer"))
				.thread(5);
		spider.run();
	}
}
